﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenPath
{
    public partial class AddNew : Form
    {
        public AddNew()
        {
            InitializeComponent();
        }
        public AddNew(Action action)
        {

            InitializeComponent();
            this.action = action;
        }
        private List<PathModle> pathModles;
        private static string jsonFileName = "list.json";
        private readonly Action action;

        private void AddNew_Load(object sender, EventArgs e)
        {
            pathModles = new List<PathModle>();
            ReNewList();
        }
        private void ReNewList()
        {
            if (File.Exists(jsonFileName))
            {
                string fileString = File.ReadAllText(jsonFileName);
                pathModles = JsonConvert.DeserializeObject<List<PathModle>>(fileString);
                ShowInView();
            }
        }

        private void ShowInView()
        {

            if (pathModles != null)
            {
              listBoxShow.Items.Clear();
                pathModles.ForEach((p) => {
                    listBoxShow.Items.Add(p);
                });
            }

        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBoxName.Text ))
            {
                MessageBox.Show("请输入名称");
                return;
            }
            if (!Directory.Exists(textBoxPath.Text))
            {
                MessageBox.Show("请输入正确的路径");
                return;

            }

           
           
            if (pathModles.Any(p => p.Name == textBoxName.Text))
            {
                var eidtPathModel = pathModles.Find(p => p.Name == textBoxName.Text);
                eidtPathModel.Path = textBoxPath.Text;
                
            }
            else if(pathModles.Any(p => p.Path == textBoxPath.Text))
            {
                var eidtPathModel = pathModles.Find(p => p.Path == textBoxPath.Text);
                eidtPathModel.Name = textBoxName.Text;
            }
            else
            {
                PathModle pathModle = new PathModle { Name = textBoxName.Text, Path = textBoxPath.Text };
                pathModles.Add(pathModle);
            }
            textBoxName.Clear();
            textBoxPath.Clear();
            ShowInView();

        }

        private void listBoxShow_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            try
            {
                PathModle pathModle = (PathModle)listBoxShow.SelectedItem;

                textBoxName.Text = pathModle.Name;
                textBoxPath.Text = pathModle.Path;
            }
            catch  
            {

                
            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PathModle pathModle = (PathModle)listBoxShow.SelectedItem;
            pathModles.Remove(pathModle);
            ShowInView();
        }

        private void textBoxPath_DoubleClick(object sender, EventArgs e)
        {
            if (fbd.ShowDialog()==DialogResult.OK)
            {
                textBoxPath.Text = fbd.SelectedPath;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            //
            string fileString = JsonConvert.SerializeObject(pathModles);
            File.WriteAllText(jsonFileName,fileString);
            action.Invoke();
        }
    }
}
